﻿using GalaSoft.MvvmLight.Messaging;
using KINLO.Model.MES;
using KINLO.UI.Main.Hepler;
using KINLO.WebApi.MESModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KINLO.UI.Main.Services
{
    /// <summary>
    /// 补录类消息注册
    /// </summary>
    public class MESEntryDataService
    {
        object _token;
        MESProductionHourData HourDataLogList { get; set; }
        MESProductionDayData DayDataLogList { get; set; }
        MESEquipmentStatuLogData EquipmentStatLogList { get; set; }
        public MESEntryDataService()
        {

            HourDataLogList = new MESProductionHourData() ;
            HourDataLogList.Load();
            DayDataLogList =new MESProductionDayData() ;
            DayDataLogList.Load();
            EquipmentStatLogList = new MESEquipmentStatuLogData();
            EquipmentStatLogList.Load();

            //生产统计小时接口调用失败补录信息存
            Messenger.Default.Register(this, "HourData", new Action<MESProduct_HourDataModel>((s) => HourData_Entry(s)));

            //生产统计天接口调用失败补录信息存
            Messenger.Default.Register(this, "DayData", new Action<MESProduction_DayDataModel>((s) => DayData_Entry(s)));
            //设备履历接口调用失败补录信息存
            Messenger.Default.Register(this, "EquipmentStatus", new Action<List<MESEquipmentStatusModel>>((s) => EquipmentStatus_Entry(s)));

        }
        object lockHour = new object();
        public void HourData_Entry(MESProduct_HourDataModel _data)
        {
            try
            {
                lock (lockHour)
                {
                    HourDataLogList.DataList.Add(new MESProduction_HourDataLogModel()
                    {
                        ID = Guid.NewGuid(),
                        Result = "失败",
                        UploadedDate = DateTime.Now,
                        Team = _data.Team,
                        Shift = _data.Shift,
                        WorkNumber = _data.WorkNumber,
                        EquipmentNo = _data.EquipmentNo,
                        WorkOrderNo = _data.WorkOrderNo,
                        PutIntoNumber = _data.PutIntoNumber.ToString(),
                        QualifiedNumber = _data.QualifiedNumber.ToString(),
                        UnqualifiedNumber = _data.UnqualifiedNumber.ToString(),
                        ProductionStartTime = DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:mm:ss"),
                        ProductionEndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    });
                }
            }
            catch (Exception ex)
            {

            }
        }
        object lockDay = new object();
        public void DayData_Entry(MESProduction_DayDataModel _data)
        {
            try
            {
                lock (lockDay)
                {
                    DayDataLogList.DataList.Add(new MESProduction_DayDataLogModel()
                    {
                        ID = Guid.NewGuid(),
                        Result = "失败",
                        UploadedDate = DateTime.Now,
                        Team = _data.Team,
                        Shift = _data.Shift,
                        WorkNumber = _data.WorkNumber,
                        EquipmentNo = _data.EquipmentNo,
                        WorkOrderNo = _data.WorkOrderNo,
                        PutIntoNumber = _data.PutIntoNumber.ToString(),
                        QualifiedNumber = _data.QualifiedNumber.ToString(),
                        UnqualifiedNumber = _data.UnqualifiedNumber.ToString(),
                    });
                }
            }
            catch (Exception ex)
            {

            }
        }
        object lockEquipmentStatus = new object();
        public void EquipmentStatus_Entry(List<MESEquipmentStatusModel> _data)
        {
            try
            {
                lock (lockEquipmentStatus)
                {
                    EquipmentStatLogList.DataList.Add(new MESEquipmentStatuLogDataModel()
                    {
                        ID = Guid.NewGuid(),
                        Result = "失败",
                        UploadedDate = DateTime.Now,
                        data = _data
                    });
                }
            }
            catch (Exception ex)
            {

            }
        }

    }
}
